export default function debounce<T extends (...args: any) => any>(
  fn: T,
  duration = 100,
): T {
  let timer: ReturnType<typeof setTimeout> | undefined;
  return function (this: any, ...args: any) {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn.apply(this, args);
      timer = undefined;
    }, duration);
  } as T;
}
